﻿function showMsgBox(username) {
	if (refreshTab('showMsgBox_' + username)) return;
	var pageSize = 20;

	var store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : url.getMsgList
						}),
				reader : new Ext.data.JsonReader({
							root : 'result',
							totalProperty : 'total',
							id : 'mid'
						}, ['mid', 'title', 'username', 'nickname', 'time', 'read'])
			});
	store.load({
				params : {
					start : 0,
					limit : pageSize,
					username : username
				}
			});

	var cm = new Ext.grid.ColumnModel([{
		header : 'Title',
		sortable : false,
		dataIndex : 'title',
		renderer : function(value, p, record) {
			return String
					.format(
							'<a href="javascript:void(0);" onclick="showMsg({1});" title="{0}" class="likelink" style="{2}">{0}</a>',
							value, record.data.mid, record.data.read ? '' : 'font-weight:bold');
		}
	}, {
		header : 'From',
		sortable : false,
		dataIndex : 'username',
		width : 30,
		renderer : sys.rd.author
	}, {
		header : 'Time',
		sortable : false,
		dataIndex : 'time',
		width : 30
	}]);

	var toolbar = new Ext.Toolbar({
				items : [new Ext.Button({
									text : '发新消息',
									iconCls : 'newmail-icon',
									handler : function() {
										sendMsg();
									},
									disabled : !sys.canSendMsg
								}), new Ext.Toolbar.Separator(), new Ext.Button({
									text : '删除',
									iconCls : 'delete-icon',
									disabled : true,
									handler : ondel
								}), new Ext.Button({
									text : '回复',
									iconCls : 'replymail-icon',
									disabled : true,
									handler : onreply
								}), new Ext.Toolbar.Separator(), new Ext.Button({
									text : '清空',
									iconCls : 'clean-icon',
									handler : function() {
										cleanMsgBox(username);
									}
								})]
			});

	var sm = new Ext.grid.RowSelectionModel({
				singleSelect : true
			});
	sm.on('selectionchange', function() {
				var record = getSelect();
				toolbar.get(2).setDisabled(!record);
				toolbar.get(3).setDisabled(!(record && sys.canSendMsg));
			});

	var pagebar = new Ext.PagingToolbar({
				pageSize : pageSize,
				store : store,
				displayInfo : true,
				emptyMsg : '目前没有消息。',
				displayMsg : '当前显示最近 {0} - {1} 条消息，共 {2} 条消息。',
				plugins : new Ext.ux.SlidingPager()
			});

	var grid = new Ext.grid.GridPanel({
				title : '收件箱' + (!sys.god ? '' : ' - ' + username),
				id : 'showMsgBox_' + username,
				iconCls : 'mail-icon',
				closable : true,
				region : 'center',
				loadMask : {
					msg : '载入中...'
				},
				viewConfig : {
					forceFit : true
				},
				store : store,
				cm : cm,
				sm : sm,
				tbar : toolbar,
				bbar : pagebar
			});
	grid.on('rowdblclick', ondblclick);
	grid.onRefresh = onRefresh;
	addTab(grid);

	function onRefresh() {
		store.reload();
	}
	function getSelect() {
		if (sm.getCount() == 0) return null;
		return sm.getSelected();
	}
	function ondel() {
		var select = getSelect();
		if (!select) return;
		deleteMsg({
					mid : select.data.mid,
					title : select.data.title,
					success : onRefresh
				})
	}
	function onreply() {
		var select = getSelect();
		if (!select) return;
		sendMsg({
					username : select.data.username,
					title : 're:' + select.data.title
				});
	}
	function ondblclick() {
		var record = getSelect();
		if (!record) return;
		showMsg(record.data.mid);
	}
}

function deleteMsg(obj) {
	obj = defaultx(obj, 'mid');
	confirmx('您确定要删除消息 [' + obj.title + '] 吗？', function() {
				ajax({
							url : url.deleteMsg,
							params : {
								mid : obj.mid
							},
							success : obj.success
						});
			});
}

function cleanMsgBox(obj) {
	obj = defaultx(obj, 'username');
	confirmx('您确定要清空' + (sys.god ? '用户 [ ' + obj.username + ' ] 的' : '') + '收件箱吗？', function() {
				ajax({
							url : url.cleanMsgBox,
							params : {
								username : obj.username
							},
							success : function() {
								refreshTab('showMsgBox_' + obj.username);
								if (obj.success) obj.success();
							}
						});
			});
}

function inquiryNewMsg() {
	ajax({
				url : url.inquiryNewMsg,
				success : function(obj) {
					if (sys.newmsg != obj.count) {
						sys.newmsg = obj.count;
						refreshStatusBar();
					}
				},
				failure : function() {}
			});
}
